<?php
$pagetitle="Server Browser";
$extrahead='<script src="sorttable.js"></script>';
include('header.php');

include('functions.inc.php');

include('data.inc.php');

showCrumbs('browse');

$path=$_SERVER['PATH_INFO'];

$scanid=$_GET['scanid']?$_GET['scanid']:getLastScan();
$hostid=$_GET['hostid']?$_GET['hostid']:-1;
$pathid=$_GET['pathid']?$_GET['pathid']:-1;

function displayNestedPath($scanid, $hostid, $pathid)
{
	$ret="";
	$output="";
	while ($pathid!=null && $pathid!=-1)
	{
		$pathinfo=getPathInfo($scanid, $pathid);
		$pathname=$pathinfo['name'];
		$quotedpathname=htmlspecialchars($pathname);
		$link="?scanid=$scanid&amp;hostid=$hostid&amp;pathid=$pathid";
		$output=" <li>&raquo; <a href=\"$link\">$quotedpathname</a></li>\n".$output;
		$ret="/$pathname".$ret;
		$pathid=$pathinfo['parentpathid'];
	}
	if ($hostid!=-1)
	{
		$hostinfo=getHostInfo($scanid, $hostid);
		$hostname=$hostinfo['name'];
		$quotedhostname=htmlspecialchars($hostname);
		$link="?scanid=$scanid&amp;hostid=$hostid";
		$output=" <li>&raquo; <a href=\"$link\">$quotedhostname</a></li>\n".$output;
		$ret=$hostname.$ret;
	}
	$link="?scanid=$scanid";
	$output=" <li><a href=\"$link\">Top</a></li>\n".$output;
	print "<ul id=\"parentpaths\">\n";
	print $output;
	print "</ul>\n";
	return $ret;
}

function displayHeader($type)
{
	print " <thead>\n";
	print "  <tr>\n";
	print "   <td></td>\n";
	print "   <td>Name</td>\n";
	print "   <td>Size</td>\n";
	if ($type=='hosts' || $type=='dirs')
	{
		print "   <td># Files</td>\n";
	}
	else if ($type=='files')
	{
		print "   <td></td>\n";
	}
	print "  </tr>\n";
	print " </thead>\n";
}
function displayObjects($type, $scanid, $hostid=-1, $objects)
{
	foreach ($objects as $object)
	{
		print "  <tr>\n";
		$name=htmlspecialchars($object['name']);
		if ($type=='hosts' || $type=='dirs')
		{
			$id=$object['id'];
			$realsize=$object['totalsize'];
			$size=size_readable($object['totalsize']);
			$realsubobjects=$object['numsharedfiles'];
			$subobjects=number_format($object['numsharedfiles']);
			if ($type=='hosts')
				$link="?scanid=$scanid&amp;hostid=$id";
			else
				$link="?scanid=$scanid&amp;hostid=$hostid&amp;pathid=$id";
		}
		else if ($type=='files')
		{
			$realsize=$object['filesize'];
			$size=size_readable($object['filesize']);
		}
		$icon=substr($type, 0, -1);
		print "   <td><img src=\"img/$icon.png\" /></td>\n";
		if (isset($link))
			print "   <td><a href=\"$link\">$name</a></td>\n";
		else
			print "   <td>$name</td>\n";
		print "   <td style=\"text-align:right\" sorttable_customkey=\"$realsize\">$size</td>\n";
		if (isset($subobjects))
			print "   <td style=\"text-align:right\" sorttable_customkey=\"$realsubobjects\">$subobjects</td>\n";
		else
			print "   <td></td>\n";
		print "  </tr>\n";
	}
}

$curpath=displayNestedPath($scanid, $hostid, $pathid);
print "<table id=\"directorylist\" class=\"sortable\">\n";
if ($hostid==-1)
{
	displayHeader('hosts');
	$objects=getAllHostStats($scanid);
	displayObjects('hosts', $scanid, -1, $objects);
}
elseif ($pathid==-1)
{
	displayHeader('dirs');
	$objects=getHostTopDirStats($scanid, $hostid);
	displayObjects('dirs', $scanid, $hostid, $objects);
}
else
{
	displayHeader('dirs');
	$objects=getPathSubdirs($scanid, $pathid);
	displayObjects('dirs', $scanid, $hostid, $objects);
	
	$objects=getPathFiles($scanid, $pathid);
	displayObjects('files', $scanid, $hostid, $objects);
}
print "</table>\n";
include('footer.php');
hitPageCounter($mysql,"browse",substr($curpath,0,255));
?>
